﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;

namespace ProgNetComponentsDx.Language.Design.Editors
{
    class RadioItemLngSettingsCollectionEditor : CollectionEditor
    {
        public RadioItemLngSettingsCollectionEditor()
            : base(typeof(RadioItemLngSettingsCollection))
        {
            
        }

        RadioItemLngSettingsCollection collection = null;
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            RepositoryItemRadioGroup rep = null;
           // context.Instance.AsString().LogToFile();

            if (context != null && context.Instance != null)
            {
                LanguageSettings set = context.Instance as LanguageSettings;
                if (set != null)
                {
                    if (set.Owner is RepositoryItemRadioGroup)
                        rep = set.Owner as RepositoryItemRadioGroup;
                    else if (set.Owner is RadioGroup)
                        rep = (set.Owner as RadioGroup).Properties;
                }
            }
            collection = value as RadioItemLngSettingsCollection;
            if (collection != null)
            {
                if (rep != null)
                {
                    List<object> list = new List<object>();

                    bool isNull = false;
                    foreach (RadioGroupItem item in rep.Items)
                    {
                        if (item.Value != null)
                            list.Add(item.Value);
                        else
                            isNull = true;
                        RadioItemLngSettings s= collection.GetItem(item.Value);
                        if (s == null)
                        {
                            s = new RadioItemLngSettings(item.Value, "");
                            collection.Add(s);
                        }
                    }
                    List<RadioItemLngSettings> strings = new List<RadioItemLngSettings>();
                    foreach (RadioItemLngSettings s in collection)
                    {
                        if (s.ItemValue != null)
                        {
                            if (!list.Contains(s.ItemValue))
                            {
                                strings.Add(s);
                            }
                        }
                    }
                    if (!isNull)
                    {
                        RadioItemLngSettings s = collection.GetItem(null);
                        if (s != null)
                            collection.Remove(s);
                    }
                    foreach (RadioItemLngSettings s in strings)
                        collection.Remove(s);
                }
            }
            object o= base.EditValue(context, provider, value);
            if (collection != null && rep!=null)
            {
                foreach (RadioItemLngSettings s in collection)
                {
                    if (s.StringCode.IsNotNull())
                    {
                        foreach (RadioGroupItem item in rep.Items)
                        {
                            if (item.Value.EqualsSafe(s.ItemValue))
                            {
                                item.Description = ProgNetLanguage.sGetString(s.StringCode);
                            }
                        }
                    }
                }
            }
            return o;
        }

        protected override object CreateInstance(Type itemType)
        {
            object obj =  new RadioItemLngSettings();
            if (obj != null && collection !=null)
            {
                if (obj is RadioItemLngSettings)
                {
                    (obj as RadioItemLngSettings).Owner = collection.Owner;
                }
            }
            return obj;
        }

        protected override bool CanRemoveInstance(object value)
        {
            return false;
        }
        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm f = base.CreateCollectionForm();
            try
            {
                f.Controls["overArchingTableLayoutPanel"].Controls["addRemoveTableLayoutPanel"].Controls.Clear();
                f.Controls["overArchingTableLayoutPanel"].Controls.Remove(f.Controls["overArchingTableLayoutPanel"].Controls["downButton"]);
                f.Controls["overArchingTableLayoutPanel"].Controls.Remove(f.Controls["overArchingTableLayoutPanel"].Controls["upButton"]);
            }
            catch { }
            return f;
        }
    }
}
